package com.adaptiveAgingAdmin.code;

import com.adaptiveAgingAdmin.utils.GsonUtil;
import lombok.AllArgsConstructor;
import lombok.Data;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Data
@AllArgsConstructor
public class Result {

    private Integer code;
    private Object data;
    private String msg;

    public static Result OK(Object data, String msg){
        return new Result(Code.OK,data,msg);
    }
    public static Result queryOK(Object data){
        return new Result(Code.OK,data,"查询成功!");
    }
    public static Result insertOK(){
        return new Result(Code.OK,null,"添加成功!");
    }
    public static Result updateOK(){
        return new Result(Code.OK,null,"修改成功!");
    }
    public static Result deleteOK(){
        return new Result(Code.OK,null,"删除成功!");
    }
    public static Result ERR(Object data,String msg){
        return new Result(Code.ERR,data,msg);
    }
    public static Result queryERR(Object data){
        return new Result(Code.ERR,data,"查询失败!");
    }
    public static Result insertERR(){
        return new Result(Code.ERR,null,"添加失败!");
    }
    public static Result updateERR(){
        return new Result(Code.ERR,null,"修改失败!");
    }
    public static Result deleteERR(){
        return new Result(Code.ERR,null,"删除失败!");
    }
    public static Result EXCEPTION(String msg){
        return new Result(Code.EXCEPTION,null,msg);
    }
    public static Result NOT_LOGIN(String msg) {
        return new Result(Code.NOT_LOGIN, null, msg);
    }

    public static void exceptionResponse(HttpServletResponse response,Integer code,Object data,String msg) throws IOException {
        response.setStatus(200);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(GsonUtil.objectToJson(new Result(code,data,msg)));
    }
}
